<template>

	<!-- 商品子项 -->
	<view class="goods-item">

		<!-- 子项左侧图片-->
		<view class="goods-item-left">
			<!-- 数据操作视图  这里犯了个错误：数据已经修改为活的了 但视图使用的还是死数据 切记-->
			<radio :checked="goods.goods_state" color="#c00000" v-if="showRadio" @click="radioClickHandler"></radio>
			<image :src="goods.goods_small_logo || defaultPic " class="goods-pic"></image>
		</view>

		<!-- 子项右侧名称&&价格 -->
		<view class="goods-item-right">
			<view class="goods-name">
				{{goods.goods_name||''}}
			</view>
			<view class="goods-info-box">
				<view class="goods-price" v-if="goods.goods_price!==Number">
					￥{{goods.goods_price | toFixed }}
				</view>
				<uni-number-box :value="goods.goods_count" :min=" 1" v-if="showNumBox" @change="numberChangeHandler">
				</uni-number-box>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		name: "my-goods",
		props: {
			goods: {
				type: Object,
				default: {}
			},
			showRadio: {
				type: Boolean,
				default: false
			},
			showNumBox: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				// 默认的空图片
				defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
			};
		},
		filters: {
			toFixed(num) {
				return Number(num).toFixed(2)
			}
		},
		methods: {
			radioClickHandler() {
				this.$emit('radio-change', {
					goods_id: this.goods.goods_id,
					// 取反后的radio状态即最新值
					goods_state: !this.goods.goods_state
				})
			},
			// val：uni-number-box封装好的 最新输入框的数值
			numberChangeHandler(val) {
				this.$emit('number-change', {
					goods_id: this.goods.goods_id,
					// 赋最新值
					goods_count: +val
				})
			}
		}
	}
</script>

<style lang="scss">
	.goods-item {
		// 让 goods-item 项占满整个屏幕的宽度
		width: 750rpx;
		// 设置盒模型为 border-box
		box-sizing: border-box;
		display: flex;
		padding: 10px 5px;
		border-bottom: 1px solid #f0f0f0;

		.goods-item-left {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-right: 5px;

			.goods-pic {
				width: 100px;
				height: 100px;
				display: block;
			}
		}

		.goods-item-right {
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.goods-name {
				font-size: 13px;
			}

			.goods-info-box {
				display: flex;
				justify-content: space-between;

				.goods-price {
					font-size: 16px;
					color: #c00000;
				}
			}

		}
	}
</style>
